package dao;

import commen.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hewen
 */

// 1.新增题目
// 2.删除题目
// 3.修改题目

// 4.查询题目列表
// 5.查询题目详情
public class ProblemDAO {

    // 1.新增题目
    public boolean insert(Problem problem) {
        System.setProperty("file.encoding", "UTF-8");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into Problems values (null,?,?,?,?,?);";
            System.out.println("插入："+sql);
            statement = connection.prepareStatement(sql);
            statement.setString(1, problem.getTitle());
            statement.setString(2, problem.getLevel());
            statement.setString(3, problem.getDescription());
            statement.setString(4, problem.getTemplateCode());
            statement.setString(5, problem.getTestCode());
            int ret = statement.executeUpdate();
            return ret == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
        return false;
    }

    // 2.删除题目
    public boolean delete(int id) {
        System.setProperty("file.encoding", "UTF-8");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from Problems where id = ?;";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            int ret = statement.executeUpdate();
            return ret == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
        return false;
    }

    // 3.修改题目
    public boolean update(Problem problem) {
        System.setProperty("file.encoding", "UTF-8");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "UPDATE Problems SET title = ?, level = ?, " +
                    "description = ?, templateCode = ?, testCode = ? " +
                    "WHERE id = ?;";
            statement = connection.prepareStatement(sql);
            statement.setString(1, problem.getTitle());
            statement.setString(2, problem.getLevel());
            statement.setString(3, problem.getDescription());
            statement.setString(4, problem.getTemplateCode());
            statement.setString(5, problem.getTestCode());
            statement.setInt(6, problem.getId());
            int ret = statement.executeUpdate();
            return ret == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
        return false;
    }

    // 4.查询题目列表
    public List<Problem> selectAll() {
        System.setProperty("file.encoding", "UTF-8");
        List<Problem> problems = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select id, title, level from Problems;";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problems.add(problem);
            }
            return problems;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return problems;
    }

    // 5.查询题目详情
    public Problem selectOne(int id) {
        System.setProperty("file.encoding", "UTF-8");
        List<Problem> problems = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Problem problem = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select id, title, level, description, templateCode from Problems where id = ?;";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problem.setDescription(resultSet.getString("description"));
                problem.setTemplateCode(resultSet.getString("templateCode"));
                return problem;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return problem;

    }
    // 6、获取某个题目的测试代码
    public String getTestCasesForTheTopic(int id) {
        System.setProperty("file.encoding", "UTF-8");
        List<Problem> problems = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Problem problem = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select testCode from Problems where id = ?;";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                return resultSet.getString("testCode");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;

    }

    public static void main(String[] args) {
        System.setProperty("file.encoding", "UTF-8");
        Problem problem = new Problem();
        ProblemDAO problemDAO = new ProblemDAO();
        problem.setId(4);
        problem.setTitle("两数之和");
        problem.setLevel("简单");
        problem.setDescription("给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target  的那 两个 整数，并返回它们的数组下标。\n" +
                "\n" +
                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
                "\n" +
                "你可以按任意顺序返回答案。\n" +
                "\n" +
                " \n" +
                "\n" +
                "示例 1：\n" +
                "\n" +
                "输入：nums = [2,7,11,15], target = 9\n" +
                "输出：[0,1]\n" +
                "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n" +
                "示例 2：\n" +
                "\n" +
                "输入：nums = [3,2,4], target = 6\n" +
                "输出：[1,2]\n" +
                "示例 3：\n" +
                "\n" +
                "输入：nums = [3,3], target = 6\n" +
                "输出：[0,1]\n" +
                " \n" +
                "\n" +
                "提示：\n" +
                "\n" +
                "2 <= nums.length <= 104\n" +
                "-109 <= nums[i] <= 109\n" +
                "-109 <= target <= 109\n" +
                "只会存在一个有效答案\n" +
                " \n" +
                "\n" +
                "进阶：你可以想出一个时间复杂度小于 O(n2) 的算法吗？");
        problem.setTemplateCode("import java.util.*;\n" +
                "class Solution {\n" +
                "    public int[] twoSum(int[] nums, int target) {\n" +
                "\n" +
                "    }\n" +
                "}");
        problem.setTestCode("    public static void main(String[] args) {\n" +
                "        Solution solution = new Solution();\n" +
                "        int count = 0;\n" +
                "        int total = 2;\n" +
                "        int[] nums_1 = {2, 7, 11, 15};\n" +
                "        int target_1 = 9;\n" +
                "        String anticipate_1 = \"[0, 1]\";\n" +
                "        String actual_1 = Arrays.toString(solution.twoSum(nums_1, target_1));\n" +
                "        if (!anticipate_1.equals(actual_1)) {\n" +
                "            System.out.println(\"案例未通过:\");\n" +
                "            System.out.println(\"nums = {2,7,11,15};target = 9;\");\n" +
                "            System.out.println(\"实际输出:\" + actual_1);\n" +
                "            System.out.println(\"预期输出:\" + anticipate_1);\n" +
                "            count++;\n" +
                "        }\n" +
                "        int[] nums_2 = {3, 2, 4};\n" +
                "        int target_2 = 6;\n" +
                "        String actual_2 = Arrays.toString(solution.twoSum(nums_2, target_2));\n" +
                "        String anticipate_2 = \"[1, 2]\";\n" +
                "        if (!anticipate_2.equals(actual_2)) {\n" +
                "            System.out.println(\"未通过案例:\");\n" +
                "            System.out.println(\"nums = [3, 2, 4];\\n\" +\n" +
                "                    \"target = 9;\");\n" +
                "            System.out.println(\"实际输出:\" + actual_2);\n" +
                "            System.out.println(\"预期输出:\" + anticipate_2);\n" +
                "            count++;\n" +
                "        }\n" +
                "        System.out.println((total - count) + \"/\" + total + \"个测试用例通过.\");\n" +
                "    }");
//        problemDAO.insert(problem);
//        Problem problem1 = problemDAO.selectOne(1);
//        System.out.println(problem1);
//        problemDAO.delete(2);
//        problemDAO.delete(3);
//        System.out.println(problemDAO.selectOne(4));
        problemDAO.update(problem);
        System.out.println(problemDAO.selectOne(4));

    }
}
